package com.smalljj.dao.impl;

import java.util.List;

import com.smalljj.bean.Article;
import com.smalljj.dao.ArticleDao;

import finaler.util.Page;
import finaler.util.PageList;
import finaler.util.SQLUtil;

public class ArticleDaoImpl extends MiddleAbstractDao<Article> implements ArticleDao {
	
	@Override
	protected Class<Article> getEntityClass() {
		return Article.class;
	}

	@Override
	public PageList getList(Page page) throws Exception {
		StringBuffer whereSql = new StringBuffer();
		if(page.getParameter("newsKey")!=null && page.getParameter("newsKey").toString().equals("")==false){
			whereSql.append(" and  title like '%" + page.getParameter("newsKey") +"%'");
			whereSql.append(" or comment like '%" + page.getParameter("newsKey") +"%'");
		}
		if(page.getParameter("userId")!=null && page.getParameter("userId").toString().equals("")==false){
			whereSql.append(" and userid = "+page.getParameter("userId"));
		}
		PageList pageList = new PageList(page);//固定
		StringBuffer sqlCount = new StringBuffer();
		sqlCount.append("select count(1) from article where 1=1 ");
		sqlCount.append( whereSql );
		int dataCount = super.getIntBySql( sqlCount.toString() );//取总
		pageList.setTotal(dataCount);//总数
		
		StringBuffer queryListSql = new StringBuffer();
		queryListSql.append(" select *");
		queryListSql.append(" from article where 1=1 ");
		queryListSql.append( whereSql );
		queryListSql.append(" order by id desc");
		SQLUtil pageSQL = new SQLUtil(queryListSql);//固定
		pageSQL.setFirstResult( page.getStart() );//固定
		pageSQL.setMaxResults( page.getRange() );//固定
		List<Article> dataList = super.findBySqlToBeanList(pageSQL.getSql());//数据集
		pageList.setList(dataList);
		return pageList;
	}
}
